package com.example.shushiyunsst.fragment.main.home.child.icon_sst

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.library_common.base.LazyFragment
import com.example.library_common.global.AppGlobals
import com.example.library_common.utils.even.ICON_TRANSFER_NOTIFY
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.even.SHOW_BUYING_PAGE_CML
import com.example.library_common.view.badge_view.QBadgeView
import com.example.library_common.view.bottombar.BottomBorInfo
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.icon_sst.bean.CurrencyTransferJavaParent
import com.example.shushiyunsst.fragment.main.home.child.icon_sst.bean.PushMsgManager
import com.example.shushiyunsst.util.CURRENCY_TRANSFER
import com.example.shushiyunsst.widget.lab_layout.JssFixTabLayout
import com.example.shushiyunsst.widget.lab_layout.OnTabSelectListener
import com.example.wallet.db.UserInfoManager
import com.google.android.material.tabs.TabLayout


/**
 * 货币交易
 * */
@Route(path = CURRENCY_TRANSFER)
class CurrencyTransferFragment : LazyFragment(), BottomBorInfo, CurrencyTransferJavaParent,
    OnTabSelectListener, View.OnClickListener {

    companion object {
        fun newInstance(): CurrencyTransferFragment {
            val args = Bundle()

            val fragment = CurrencyTransferFragment()
            fragment.arguments = args
            return fragment
        }


    }

    override val title: Int = R.string.wallet_sst

    override val icon: Int = R.mipmap.icon_sst_1



    private val titles = listOf(
        R.string.sst_transfer_buying_lab,
        R.string.sst_transfer_sell_lab
    )

    private val tag = listOf(
        ICON_TRANSFER_SETTING_BUY,
        ICON_TRANSFER_SETTING_SELL
    )

    private var iconBadgeView: QBadgeView? = null

    private lateinit var mTabLayout: JssFixTabLayout

    private lateinit var mSubTabLayout: TabLayout

    private lateinit var mViewPager: ViewPager

    private var myOrderTv: View? = null

    private var pagerAdapter: FragmentStatePagerAdapter? = null

    private var settingBtn: View? = null

    private val subFragments = arrayOf<Fragment>(
        SstTransferFragment.newInstance().setTransferParent(this),
//        CMLTransferFragment.newInstance().setTransferParent(this)
    )

    override fun getLayoutId() = R.layout.currency_transfer_fragment

    override fun lazyInit() {

    }

    override fun initView(view: View) {


        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.sst_transfer)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mTabLayout = view.findViewById(R.id.mTabLayout)

        for (e in titles) {

            mTabLayout.addNewTab(e)
        }



        mTabLayout.setOnTabSelectListener(this)

        mSubTabLayout = view.findViewById(R.id.mSubTabLayout)
        mViewPager = view.findViewById(R.id.mViewPager)

        pagerAdapter = object
            :
            FragmentStatePagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

            override fun getCount() = subFragments.size

            override fun getItem(position: Int) = subFragments[position]

            override fun getPageTitle(position: Int): CharSequence? {
                val currencyTransferLab = getItem(position) as CurrencyTransferLab
                return AppGlobals.getApplication().getString(currencyTransferLab.title)
            }

        }

        mViewPager.adapter = pagerAdapter

        mSubTabLayout.setupWithViewPager(mViewPager)

        settingBtn = view.findViewById(R.id.settingBtn)

        myOrderTv = view.findViewById(R.id.myOrderTv)


        settingBtn?.setOnClickListener(this)

        myOrderTv?.setOnClickListener(this)

    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        if (pagerAdapter!=null){
            refreshData()
        }
    }



    override fun refreshData() {
        val currentItem = mViewPager.currentItem
        val item = pagerAdapter?.getItem(currentItem) as CurrencyTransferLab
        item.upListData()
    }

    override fun handMessage(msg: MessageWrap?) {
//        super.handMessage(msg)
        Log.d("handMessage", "handMessage: ")
        when (msg?.message) {
            ICON_TRANSFER_NOTIFY -> {
                val iconPushMsgList = PushMsgManager.getInstance().iconPushMsgList
                if (iconPushMsgList.isEmpty()) {
                    hidBag()
                } else {
                    showBag()
                }
            }
            SHOW_BUYING_PAGE_CML ->{
                mViewPager.currentItem =1
            }
        }

    }


    private fun showBag() {
        val iconPushMsgList = PushMsgManager.getInstance().iconPushMsgList
        if (iconBadgeView == null) {
            iconBadgeView = QBadgeView(_mActivity);
        }
        val count = if (iconPushMsgList.size > 100) "99+" else "${iconPushMsgList.size}"
        Log.d("handMessage", "showBag: $count")
        iconBadgeView?.bindTarget(myOrderTv)

            ?.setBadgeBackgroundColor(
                ContextCompat.getColor(
                    _mActivity,
                    android.R.color.holo_red_light
                )
            )
            ?.setBadgeText(count)?.isShowShadow = true
    }

    private fun hidBag() {
        Log.d("handMessage", "hidBag:")
        iconBadgeView?.hide(true)
        iconBadgeView = null
    }

    override fun onDestroy() {
        super.onDestroy()
        mTabLayout.setOnTabSelectListener(null)
    }

    override fun onTabSelect(position: Int) {

    }

    override fun onTabReselect(position: Int) {

        mTabLayout.currentTab = position

        mTabLayout.updateTabStyles()

        val currencyTransferLab =
            pagerAdapter?.getItem(mViewPager.currentItem) as CurrencyTransferLab

        currencyTransferLab.upData()
    }

//    override val currentStatus=titles[mTabLayout.currentTab]

    override fun getCurrentStatus() = tag[mTabLayout.currentTab]

    override fun handSettingBtn(tokenType: Int) {

        val currentItem = mViewPager.currentItem
        val mTransferLab = pagerAdapter?.getItem(currentItem) as CurrencyTransferLab
        if (mTransferLab.tokenType != tokenType) return

        when (tokenType) {
            TOKEN_TYPE_SST -> {
                val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
                if (userInfo?.isBuss22() == true) {
                    settingBtn?.visibility = View.VISIBLE
                } else {
                    settingBtn?.visibility = View.GONE
                }
            }
            TOKEN_TYPE_CML -> {
                settingBtn?.visibility = View.VISIBLE
            }
        }
    }

    override fun showSettingBtn(isShow: Boolean) {
       val mLab= subFragments[mViewPager.currentItem] as CurrencyTransferLab

        if (mLab.tokenType==TOKEN_TYPE_CML){
            if (isShow){
                settingBtn?.visibility = View.VISIBLE
            }else{
                settingBtn?.visibility = View.GONE
            }
        }

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.settingBtn -> {
                val transferType = tag[mTabLayout.currentTab]
                val lab = subFragments[mViewPager.currentItem] as CurrencyTransferLab
                val icon = lab.tokenType
                start(IconTransferSettingFragment.newInstance(transferType, icon))
            }
            R.id.myOrderTv -> {
                start(MyOrderCurrencyFragment.newInstance())
            }
        }
    }

}